package com.woniuxy.factory.controller;

import com.woniuxy.factory.entity.Purchaseplan;
import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.exception.BusinessException;
import com.woniuxy.factory.service.PurchaseplanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
public class PurchaseplanController {
    @Autowired
    PurchaseplanService purchaseplanService;

    //获取所有采购计划
    @GetMapping("/purchaseplan")
    @ResponseBody
    public ResponseResult<List<Purchaseplan>> getpurchaseplans(Purchaseplan purchaseplan, int page, int limit){
        return purchaseplanService.getpurchaseplans(purchaseplan,page,limit);

    }
    //获取指定采购计划
    @PostMapping("/getpurchaseplanById")
    @ResponseBody
    public ResponseResult<Map> getpurchaseplanById(String pupId){
        return purchaseplanService.getpurchaseplanById(pupId);
    }
    //修改采购计划
    @ResponseBody
    @PostMapping("/updatePurchaseplan")
    public ResponseResult updatePurchaseplan(Purchaseplan purchaseplan,String empId){
        ResponseResult responseResult = new ResponseResult();
        try {
            responseResult = purchaseplanService.updatePurchaseplan(purchaseplan,empId);
            return responseResult;
        }catch (BusinessException e){
            responseResult.setMsg(e.getMessage());
        }
        return responseResult;
    }
    //删除采购计划
    @ResponseBody
    @RequestMapping("/deletepurchaseplan")
    public ResponseResult deletepurchaseplan(String pupId){
        if (pupId==""||pupId==null) {
            return new ResponseResult(1, "该计划不存在", null, null);
        }
        return  purchaseplanService.deletepurchaseplan(pupId);

    }
    //新增采购计划
    @ResponseBody
    @RequestMapping("/addPurchasePlan")
    public ResponseResult addPurchasePlan(Purchaseplan purchaseplan, String empId, HttpServletRequest request){
        return purchaseplanService.addPurchasePlan(purchaseplan,empId,request);
    }

}
